package eu.web;

import java.util.Hashtable;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import eu.integration.IntegrationNet;
import eu.template.TemplateContext;
import eu.web.frame.Constants;
import eu.common.config.ConfigEngine;
import eu.common.toolkits.ResourceUtil;

/**
 * 服务器上下文，包括一些常用配置，坏境变量等。有三个来源，serverContext.xml配置文件、EnvSetFilter、
 * StartupListener
 * 
 * @author WZZ
 * 
 */
public class AppContext {

	private static ThreadLocal<HttpServletRequest> request = new ThreadLocal<HttpServletRequest>();

	private static ThreadLocal<HttpServletResponse> response = new ThreadLocal<HttpServletResponse>();

	private static ServletContext context = null;

	private static Map<String, String> config = new Hashtable<String, String>();

	private static IntegrationNet inet = new IntegrationNet();

	/**
	 * 
	 * 
	 * @return
	 */
	public static HttpServletRequest getRequest() {
		return request.get();
	}

	/**
	 * 
	 * @return
	 */
	public static HttpSession getSession() {
		return request.get().getSession();
	}

	/**
	 * 
	 * @return
	 */
	public static HttpServletResponse getResponse() {
		return response.get();
	}

	/**
	 * 
	 * @param request
	 */
	public static void setRequest(HttpServletRequest request) {
		AppContext.request.set(request);
	}

	/**
	 * 
	 * @param response
	 */
	public static void setResponse(HttpServletResponse response) {
		AppContext.response.set(response);
	}

	/**
	 * 获取应用Context
	 * 
	 * @return
	 */
	public static ServletContext getContext() {
		return context;
	}

	/**
	 * 获取应用Base URL
	 * 
	 * @return
	 */
	public static String getAppBase() {
		return (String) getRequest().getAttribute(Constants.ENV.APP_BASE_URL);
	}

	/**
	 * 初始化Context
	 * 
	 * @param context
	 */
	public static void initContext(ServletContext context) {
		AppContext.context = context;
		ConfigEngine engine = ConfigEngine.getEngine(
				ConfigEngine.EngineType.XMLEngine,
				ResourceUtil.getResourcePath("eu.context.xml"), "UTF-8");
		// 解析配置文件
		for (String key : engine.getSubNames("/context/config")) {
			String value = engine.getValue("/context/config/" + key);
			if (value == null)
				continue;
			if (value.contains("${")) {
				value = praseVar(value);
			}
			config.put(key, value);
		}
		// 初始化PageContext
		TemplateContext.initContext();
		//
		inet.initFromConf();
	}

	private static String praseVar(String express) {
		String reg = "\\$\\{[^\\{\\}]+\\}";
		Pattern pattern = Pattern.compile(reg);
		Matcher match = pattern.matcher(express);
		while (match.find()) {
			String str = match.group();
			if ("${app_root}".equals(str)) {
				express = express.replace(str, context.getRealPath("/"));
			} else {
				express = express.replace(str,
						config.get(str.replace("${", "").replace("}", "")));
			}
		}
		return express;
	}

	public static String getConfig(String name) {
		return config.get(name);
	}

	public static boolean containsConfig(String name) {
		return config.containsKey(name);
	}

}
